class Solution {
public:
    int change(int num)
    {
        int rnum = 0;

        while (num)
        {
            int n = num % 10;
            rnum += n * n;
            num /= 10;
        }

        return rnum;
    }

    bool isHappy(int n)
    {
        int slow = n;
        int fast = n;

        slow = change(slow);
        fast = change(fast);
        fast = change(fast);

        while (slow != fast)
        {
            slow = change(slow);
            fast = change(fast);
            fast = change(fast);
        }

        if (slow == 1)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
};